package server;

import gen.User;
import gen.UserNotFound;
import gen.UserService;

import java.util.ArrayList;
import java.util.List;

import org.apache.thrift.TException;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;

/**
 * @author wenxiubi
 * 这样我就可以证明在job-search中的调用问题,不在使用server.java
 */
public class UserServiceHandler implements UserService.Iface {
	@Override
	public User getUser(String loginName) throws UserNotFound, TException {
		if (!"login1".equals(loginName)) {
			UserNotFound e = new UserNotFound("User not Found!");
			throw e;
		}
		User user = new User();
		user.setUserId(100);
		user.setLoginName("login1");
		user.setPassword("pwd1");
		user.setName("user1");
		return user;
	}

	@Override
	public List<User> getUsers() throws TException {
		List<User> list = new ArrayList<User>();
		User user = new User();
		user.setUserId(100);
		user.setLoginName("login1");
		user.setPassword("pwd1");
		user.setName("user1");
		list.add(user);
		User user2 = new User();
		user2.setUserId(200);
		user2.setLoginName("login2");
		user2.setPassword("pwd2");
		user2.setName("user2");
		list.add(user2);
		return list;
	}
	public void run(){

		try {
			UserService.Processor processor = new UserService.Processor(this);
			TServerTransport serverTransport = new TServerSocket(9090);

			Args args2 = new Args(serverTransport);
			args2.processor(processor);
			TServer server = new TSimpleServer(args2);

			// Use this for a multithreaded server
			// server = new TThreadPoolServer(processor, serverTransport);
			System.out.println("Starting the server...");
			server.serve();
		} catch (Exception x) {
			x.printStackTrace();
		}
		System.out.println("done.");
	
	}
	public static void main(String[] args) {
		UserServiceHandler handler = new UserServiceHandler();
		handler.run();
	}
}